package nc.bs.pub.action;

import nc.bs.bd.bom.bom0202.plugin.bpplugin.BomPluginPoint;
import nc.bs.bd.bom.bom0202.rule.BomBillStatusApproveCheckRule;
import nc.bs.bd.bom.bom0202.rule.BomChangeVOStatus2UpdateRule;
import nc.bs.framework.common.NCLocator;
import nc.bs.pubapp.pf.action.AbstractPfAction;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.itf.bd.bom.bom0202.IBomBillMaintainService;
import nc.itf.okws.pub.IMdmBaseService;
import nc.vo.bd.bom.bom0202.entity.AggBomVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.vo.mmbd.utils.power.MMDataPermissionUtil;

public class N_19B1_APPROVE extends AbstractPfAction<AggBomVO>
{
  public IBomBillMaintainService getUpdateService()
  {
    return (IBomBillMaintainService)NCLocator.getInstance().lookup(IBomBillMaintainService.class);
  }

  protected CompareAroundProcesser<AggBomVO> getCompareAroundProcesserWithRules(Object userObj)
  {
    CompareAroundProcesser processer = new CompareAroundProcesser(BomPluginPoint.APPROVE);

    addBeforeRule(processer);

    addAfterRule(processer);

    return processer;
  }

  protected AggBomVO[] processBP(Object userObj, AggBomVO[] clientFullVOs, AggBomVO[] originBills)
  { 
    AggBomVO[] returnbill = null;
    try {
      if (this.m_tmpVo.isCloudEntry)
      {
        MMDataPermissionUtil.checkPermission(clientFullVOs, "10140BOMM", "Approve", "vbillcode");
      }
      returnbill = getUpdateService().auditBom(clientFullVOs, originBills);
      NCLocator.getInstance().lookup(IMdmBaseService.class).sendBom(clientFullVOs[0], "MES");
    }
    catch (BusinessException e) {
      ExceptionUtils.wrappException(e);
    }
    return returnbill;
  }

  private void addBeforeRule(CompareAroundProcesser<AggBomVO> processer)
  {
    IRule approveStatusCheckRule = new BomBillStatusApproveCheckRule();
    processer.addBeforeRule(approveStatusCheckRule);

    IRule voStatusRule = new BomChangeVOStatus2UpdateRule();
    processer.addBeforeRule(voStatusRule);
  }

  private void addAfterRule(CompareAroundProcesser<AggBomVO> processer)
  {
  }
}